import requests
import re
import tkinter
from tkinter.messagebox import showwarning,showinfo

url = 'http://192.168.0.7:8080//services/GZJDDeviceService'
# url = 'http://10.168.3.8:8080/services/GZJDDeviceService'   # 益智
headers = {
    'Content-Type':'text/xml'
}
#
# # barCode = 'VG0015-BO0200349'
# barCode = "19DV30001"
# BTTestData = ""             # 测试用例的不良项
# TestResults = 'PASS'        # 测试结果
# orderStationId = '6'
# body = f'''
# <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
#                  xmlns:ser="http://service.jgmes.com/">
#    <soapenv:Header/>
#    <soapenv:Body>
#       <ser:NewSaveBluetoothTestData>
#          <barCode>{barCode}</barCode>
#          <BTTestData>{BTTestData}</BTTestData>
#          <TestResults>{TestResults}</TestResults>
#          <orderStationId>{orderStationId}</orderStationId>
#       </ser:NewSaveBluetoothTestData>
#    </soapenv:Body>
# </soapenv:Envelope>
# '''
#
# res = requests.post(url,headers=headers,data=body)
# print(res.status_code)
# print(res.text)


def NewSaveBluetoothTestData(url=url, barCode='', BTTestData='', TestResults='pass', stationID=0):
    body = f'''
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                     xmlns:ser="http://service.jgmes.com/">
       <soapenv:Header/>
       <soapenv:Body>
          <ser:NewSaveBluetoothTestData>
             <barCode>{barCode}</barCode>
             <BTTestData>{BTTestData}</BTTestData> 
             <TestResults>{TestResults}</TestResults> 
             <orderStationId>{stationID}</orderStationId> 
          </ser:NewSaveBluetoothTestData>
       </soapenv:Body>
    </soapenv:Envelope>
    '''
    res = requests.post(url, headers=headers, data=body)
    print(res.text)
    res_text = res.text
    isSuccess = re.findall(r"<isSuccess.*>(.*?)</isSuccess>", res_text)
    if isSuccess[0] == 'true':
        print('上传数据成功')
        return True
    elif isSuccess[0] == 'false':
        message = re.findall(r"<message.*>(.*?)</message>", res_text)
        print('上传数据失败,\n'+ message[0])
        result = showinfo('提示', '错误信息：\n'+message[0])
        print(f'提示: {result}')    # 警告：ok
        return False

if __name__ == '__main__':
    window = tkinter.Tk()
    window.withdraw()  # 隐藏主窗口
    # url = 'http://192.168.0.7:8080//services/GZJDDeviceService'
    barCode = '5TFC015HUS0A018DF180001'
    BTTestData = ""             # 测试用例的不良项
    TestResults = 'PASS'        # 测试结果
    orderStationId = '2'
    # 进行过站
    NewSaveBluetoothTestData(url, barCode,BTTestData,TestResults,orderStationId)







